package jtk.project4.fleet.screen.addEditFuelEntry;


/*
* Author : Rio Dermawan 
* Date Created : 1-April-2011
* Date Modified : 6-April-2011
* Version : 1.1
* */

import java.awt.FlowLayout;
import java.util.List;

import com.sun.corba.se.spi.orbutil.fsm.Action;

import jtk.project4.fleet.action.CancelAction;
import jtk.project4.fleet.action.CancelAddEditFuelEntryAction;
import jtk.project4.fleet.action.SaveAddEditFuelEntryAction;
import jtk.project4.fleet.action.SubmitAction;
import jtk.project4.fleet.domain.Fuel;
import jtk.project4.fleet.event.AddEditFuelEntryEvent;
import jtk.project4.fleet.event.AddMaintenanceTaskEvent;
import jtk.project4.fleet.field.AddEditFuelEntryField;
import jtk.project4.fleet.field.AddEditFuelReceiptField;
import jtk.project4.fleet.field.CustomerManagementField;


import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.FormLayout.Align;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;
	
public class AddEditFuelEntryView extends FormView {
	private PushButton saveButton;
	private PushButton savepButton;
	private PushButton cancelButton;
	private Action saveAction;
	private SubmitAction submitAction;
	private SubmitAction submitAction2;
	private CancelAction cancelAction;
	private Fuel fuel;

	public void handleEvent(AddEditFuelEntryEvent evt) {
		switch (evt.getType()) {
		case CANCEL:
			close();
			break;
			
		case SAVE:
			// check for validation errors
			List<ValidationError> errors = validate();
			if (!errors.isEmpty()) {
				// there are validation errors so do not submit...
				errors.get(0).getField().focus();
				break;
			} else {
				// submit form and close view
				submit();
				close();
				// fall-through!
			}
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		}
	}
			
	@Override
	protected void init() {
		createFields();
		createLayout();
		
	}
	
	private void createAction(){
		submitAction = new SubmitAction();
		submitAction2 = new SubmitAction();
		cancelAction = new CancelAction();
		setOnChangeAction(submitAction);
	}

	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(saveButton);
		buttons.addField(savepButton);
		buttons.addField(cancelButton);
		return buttons;
	}
	
	private GroupControl createForm() {
		GroupControl form = new GroupControl();
		form.addField(new AddEditFuelEntryField("fuel"), NORTH);
		form.addField(createButtons(), Align.RIGHT);		
		return form;
	}
	
	private void createLayout() {
		addField(createForm());
	}
	
	
	private void createFields(){
		saveButton = new PushButton(new SaveAddEditFuelEntryAction());
		savepButton = new PushButton("savepAction");
		cancelButton = new PushButton(new CancelAddEditFuelEntryAction());
	}
}
